package com.management.controller;

import com.management.po.*;
import com.management.service.*;
import com.management.vo.OrderFormVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;

/**
 * @author hrf
 * 订单列表控制器
 */

@Controller
@RequestMapping("/documents")
public class OrderFormController {

    Logger logger=Logger.getLogger(OrderFormController.class);

    @Autowired
    OrderFormService orderFormService;
    @Autowired
    CompanyService companyService;
    @Autowired
    OrderCommodityService orderCommodityService;
    @Autowired
    UserService userService;
    @Autowired
    FormStatsService formStatsService;

    @RequestMapping("/financial/orderformlist")
    public String OrderForm(int companyId,Model model){
        logger.debug("进入了订单列表控制器");
        List<OrderFormVO> list=orderFormService.findByCompanyId(companyId);
        model.addAttribute("list",list);
        return "orderformlist";
    }

    @RequestMapping("/financial/orderandcom")
    public String orderandcom(String orderFormId,Model model){
        logger.debug("进入了详情列表控制器");
        OrderFormVO orderFormVO=orderFormService.findByOrderFormId(orderFormId);
        OrderCommodity orderCommodity=orderCommodityService.findByOrderFormId(orderFormId);
        model.addAttribute("orderFormVO",orderFormVO);
        model.addAttribute("orderCommodity",orderCommodity);
        return "orderandcom";
    }

    @RequestMapping("/financial/toupdateorderform")
    public String toupdate(String orderFormId,Model model){
        logger.debug("进入了去修改订单的页面的控制器");
        OrderFormVO orderFormVO=orderFormService.findByOrderFormId(orderFormId);
        model.addAttribute("orderFormVO",orderFormVO);
        return "updateorderform";
    }

    @RequestMapping("/financial/updateorderform")
    public String update(OrderFormVO orderFormVO){
        logger.info("进入了修改订单的控制器");
        int i=orderFormService.update(orderFormVO);
        logger.debug(i);
        return "redirect:orderformlist.action?companyId="+orderFormVO.getCompanyId();
    }

    @RequestMapping("/financial/toaddorderform")
    public String toaddorderform(Model model){
        logger.debug("进入了去增加订单的控制器");
        List<Company> list=companyService.findAll();
        List<User> userList=userService.findAll();
        List<FormStats> formlist=formStatsService.findAll();
        model.addAttribute("list",list);
        model.addAttribute("userList",userList);
        model.addAttribute("formlist",formlist);
        return "addorderform";
    }

    @RequestMapping("/financial/addorderform")
    public String updateorderform(OrderForm orderForm){
        logger.debug("进入了增加订单的控制器");
        logger.debug(orderForm);
        logger.debug(orderForm.getDeliveryDate());
        logger.debug(orderForm.getOrderFormDate());
        int i=orderFormService.add(orderForm);
        logger.info("增加是否成功："+i);
        return "redirect:orderformlist.action?companyId="+orderForm.getCompanyId();
    }
}